﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace TestForm
{
    public static class FileUtils
    {
        public static Dictionary<string, string> LoadCSV(string path)
        {
            Dictionary<string, string> list = new Dictionary<string, string>();
            string spliter = ",";
            using (StreamReader sr = new StreamReader(path))
            {
                while (true)
                {
                    string line = sr.ReadLine();
                    if (string.IsNullOrEmpty(line))
                        break;

                    if (line.Contains("Pon"))
                        continue;

                    string[] ss = line.Split(new string[] { spliter }, StringSplitOptions.RemoveEmptyEntries);
                    for (int i = 1; i < ss.Length; i++)
                    {
                        list.Add(ss[0], ss[i]);
                    }
                }
            }

            return list;
        }

        // 测试一下
        public static void TestGetCurrentFileList(string dataPath, string pow = "")
        {
            var folderlist = GetFileList(dataPath);
            foreach (var folders in folderlist)
            {
                var folder = folders.Key;
                var fs = folders.Value;
                if (fs.Count > 1)
                {
                    foreach (var f in fs)
                    {
                        if (f.Contains("Param"))
                        {
                            var file = f;
                            var list = LoadCSV(file);
                            foreach (var l in list)
                            {
                                pow = l.Value;
                                var key = fs.Where(i => i.Contains(l.Key)).FirstOrDefault();

                            }
                        }
                    }
                    continue;
                }
                else
                {
                    var key = fs.FirstOrDefault();

                }

            }
        }

        static Dictionary<string, List<string>> folderFiles = new Dictionary<string, List<string>>();

        /// <summary>
        /// 递归遍历文件夹获取文件
        /// </summary>
        /// <param name="path"></param>
        public static Dictionary<string, List<string>> GetFileList(string path)
        {
            DirectoryInfo theFolder = new DirectoryInfo(path);
            var dirss = theFolder.GetDirectories();
            foreach (var file in dirss)
            {
                DirectoryInfo fileFolder = new DirectoryInfo(file.FullName);
                var files = fileFolder.GetFiles();
                var fileInfos = new List<string>();
                foreach (var f in files)
                {
                    fileInfos.Add(f.FullName);
                }
                if (!folderFiles.ContainsKey(file.Name))
                    folderFiles.Add(file.Name, fileInfos);
            }

            return folderFiles;
        }

    }

}
